package com.sinodata.bsm.inmp.finder.gab;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.inmp.finder.Finder;
import com.sinodata.bsm.inmp.util.DBConnectionUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-8-19 下午2:09:32          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class WenshiduFinder extends Finder {

    private static final Logger logger = Logger.getLogger(WenshiduFinder.class);

    @SuppressWarnings("rawtypes")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String name = (String) params.get("NAME");
        Connection connection = null;
        Statement statement = null;
        ResultSet set = null;
        connection = DBConnectionUtil.getInstance().getGabConnection();
        try {
            statement = connection.createStatement();
            set = statement.executeQuery(getSql(name));
            while (set.next()) {
                String[] res = new String[4];
                list.add(res);
                String equipNo = set.getString(1);
                String equipName = set.getString(2);
                res[0] = equipNo;//内部标示 equip_no
                res[1] = equipName;//资源名称 equip_nm
                res[2] = equipNo + "_" + equipName;//资源描述 equip_no+equip_nm
                res[3] = equipName;//设备名称 equip_nm
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                DBConnectionUtil.getInstance().returnConn(connection);
            }
        }
        return list;
    }

    private String getSql(String name) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("select ");
        buffer.append("equip_no, ");
        buffer.append("equip_nm ");
        buffer.append("from ");
        buffer.append("Equip ");
        buffer.append("where ");
        buffer.append("equip_nm like '");
        buffer.append(name);
        buffer.append("%' ");
        String sql = buffer.toString();
        logger.info("WenshiduFinder.getSql>>sql[" + sql + "]");
        return sql;
    }
}
